package compiler;

import compiler.scanner.JFlexScanner;
import compiler.scanner.Token;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
 

public class Compiler {
    
   
    /**
     * This function will run all the parts of the compiler 
     * (currently only runs the scanner, and outputs the tokens from the scanner). 
     * @param args the command line arguments 
     */
    public static void main(String[] args) {
        String fileName = JOptionPane.showInputDialog(null, "Enter the name of the file to be Compiled: ");
        try {
            BufferedReader br = new BufferedReader(new FileReader(fileName));
            JFlexScanner scanner = new JFlexScanner(br);
            
            String outputFileName = JOptionPane.showInputDialog( null, "Enter the name of the output file: ");
            File file = new File(outputFileName);
            if(!file.exists()){
                file.createNewFile();
            }
            FileWriter fw = new FileWriter(file.getName(),false);
            BufferedWriter bw = new BufferedWriter(fw);

            while(scanner.viewNextToken().getType() != Token.TokenType.EOF_TOKEN){
                bw.write(scanner.viewNextToken().getType().name());
                if(scanner.viewNextToken().getData() != null){
                    bw.write("," + scanner.viewNextToken().getData().toString());
                }
                bw.write("\n");
                scanner.getNextToken();
            }
            
            bw.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Compiler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex){
            Logger.getLogger(Compiler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
